 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# Copyright (C) YYYY Alexandros Fotiadis fotiadis90@gmail.com
# This program is free software: you can redistribute it and/or modify it 
# under the terms of the GNU General Public License version 3, as published 
# by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranties of 
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
# PURPOSE.  See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along 
# with this program.  If not, see <http://www.gnu.org/licenses/>.
### END LICENSE

from locale import gettext as _

from gi.repository import Gtk # pylint: disable=E0611
import logging
logger = logging.getLogger('android_keyboard')

from android_keyboard_lib import Window
from android_keyboard.AboutAndroidKeyboardDialog import AboutAndroidKeyboardDialog
from android_keyboard.PreferencesAndroidKeyboardDialog import PreferencesAndroidKeyboardDialog

# See android_keyboard_lib.Window.py for more details about how this class works
class AndroidKeyboardWindow(Window):
    
    __gtype_name__ = "AndroidKeyboardWindow"
    
    def finish_initializing(self, builder): # pylint: disable=E1002
        """Set up the main window"""
        super(AndroidKeyboardWindow, self).finish_initializing(builder)
        

        self.AboutDialog = AboutAndroidKeyboardDialog
        self.PreferencesDialog = PreferencesAndroidKeyboardDialog

        # Code for other initialization actions should be added here.
        self.toolb = self.builder.get_object("toolbar")
        self.context = self.toolb.get_style_context()
        self.context.add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)
        
        self.toolb2 = self.builder.get_object("toolbar2")
        self.context = self.toolb2.get_style_context()
        self.context.add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)
        
        self.StatusLab = self.builder.get_object("StatusLab")
        self.GeneralBox = self.builder.get_object("GeneralBox")
        self.deviceCombo = self.builder.get_object("deviceCombo")
        self.InputKey = self.builder.get_object("InputKey")
        
        #Buttons
        self.bmenu = self.builder.get_object("menu")
        self.bback = self.builder.get_object("back")
        self.bpower = self.builder.get_object("power")
        self.bcall = self.builder.get_object("call")
        self.bhome = self.builder.get_object("home")
        
                  
    def LoadProcess(self, widget):#On Show
        from android_keyboard_lib.OnLoad import run        
        import threading,time
        
        run(self)
            
    def LoadSelDevice(self, widget):
        from android_keyboard_lib.FuncLib import ConnectCurrentDevice,config,FinishtLoad
        if(FinishtLoad == False):return
 
        if(self.deviceCombo.get_active() == -1):
			return
        ConnectCurrentDevice(self)
        
    def BHome(self, widget):
        import os
        from android_keyboard_lib.FuncLib import config
        if(config == None):
            return
        SelectedDevice = config[self.deviceCombo.get_active()]
        DeviceCode = SelectedDevice[0]
        
        os.system("adb shell 'input keyevent 3'")

    def BCall(self, widget):
        import os
        from android_keyboard_lib.FuncLib import config
        if(config == None):
            return
        SelectedDevice = config[self.deviceCombo.get_active()]
        DeviceCode = SelectedDevice[0]
        
        os.system("adb shell 'input keyevent 5'")
            
    def BMenu(self, widget):
        from android_keyboard_lib.FuncLib import config,SendKey

        if(config == None):
            return
        else:
            SendKey(113, True, self, False)
            SendKey(113, False, self, False)
                
    def BBack(self, widget):
        from android_keyboard_lib.FuncLib import config,SendKey

        if(config == None):
            return
        else:
            SendKey(27, True, self, False)
            SendKey(27, False, self, False)
            
    def BPower(self, widget):
        import os
        from android_keyboard_lib.FuncLib import config
        if(config == None):
            return
        SelectedDevice = config[self.deviceCombo.get_active()]
        DeviceCode = SelectedDevice[0]
        
        os.system("adb shell 'input keyevent 26'")
            
    def KeyCatch(self, widget):
		HOME = _("Home")
		CALL = _("Call")
		BACK = _("Back")
		POWER = _("Power")
		MENU = _("Menu")
		
        if(self.InputKey.get_active() == 0):
            self.toolb.set_sensitive(1)
            self.toolb2.set_sensitive(1)
            
			self.bmenu.set_label(MENU)
			self.bback.set_label(BACK)
			self.bpower.set_label(POWER)
			self.bcall.set_label(CALL)
			self.bhome.set_label(HOME)
        else:
            self.toolb.set_sensitive(0)
            self.toolb2.set_sensitive(0)
            
			self.bmenu.set_label("F7")
			self.bback.set_label("Esc")
			self.bpower.set_label("F9")
			self.bcall.set_label("F6")
			self.bhome.set_label("F8")
            
    def PressedKey(self, widget, event):
        from android_keyboard_lib.FuncLib import CurrentGO,ExecuteButton
        
        if(self.InputKey.get_active() == 0):
            return
        if(CurrentGO[0] == False):
            return
            

        ExecuteButton(event, True, self)

    def ReleasedKey(self, widget, event):
        from android_keyboard_lib.FuncLib import CurrentGO,ExecuteButton,PressedKeys
        
        
        if(PressedKeys == 'C'):
            return
        
        if(self.InputKey.get_active() == 0):
            return
        if(CurrentGO[0] == False):
            return
            
        ExecuteButton(event, False, self)
        
    def DonateAktive(self, widget):
        import webbrowser
        webbrowser.open('https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=8WQ9R4Z52QCWA&lc=GR&item_name=Donate&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted')
        
	def onDestroy(self, widget):
		from android_keyboard_lib.xlikan_lib import log; logger = log()
		logger.debug("Excute onDestroy Function")
		
	
	def onShow(self, widget):
		from android_keyboard_lib.xlikan_lib import log; logger = log()
		logger.debug("Excute onSHow Function")


        
        
        
        
        
        
        

